<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Amiba;
use DB;
use Flash;
use Illuminate\Database\QueryException;
use Excel;

class AmibaController extends Controller
{
   //amiba 
    public function ajaxIndex()
    {
        $model_amiba = new Amiba();
       
      
        $model_amiba = $model_amiba->ajaxIndex();
         return json_encode($model_amiba, true);
        
    }
    //导出excel
    public function project_excel()
    {
        
    }
   //展示阿米巴报表
   public  function  index()
   {
       return  view('admin.amiba.list');
   }
   
   public function edit($id)
   {
       echo $id;exit();
      
   }
   //添加阿米巴报表统计
   public function count()
   {
       return  view('admin.amiba.all');
   }
   
   //阿米巴报表统计
   public function ajaxCount()
   {
        $model_amiba = new Amiba();
        $model_amiba = $model_amiba->ajaxCount();
        return json_encode($model_amiba, true);
   }
   
   //报表统计
   public function project()
   {
       return view('admin.amiba.project');
   }
   
   public function ajaxPorject()
   {
        $model_amiba = new Amiba();
        $model_amiba = $model_amiba->ajaxProject();
        return json_encode($model_amiba, true);
   }
   
   
   
   //添加阿米巴报表
   public function add()
   {
       
       return  view('admin.amiba.add');
   }
   
   
   public function store(Request $request)
   {
      $data['status'] = request('status','');
      $data['bar'] = request('bar','');
    
      $data['type_id'] = request('type','');
      $data['guanggao_id'] = request('zhichu','');
      $data['zixun'] = request('zixun','');
      $data['yuyue'] = request('yuyue','');
      $data['zhongzhi_yuyue'] = request('zhongzhi_yuyue','');
      $data['zhengji_yuyue'] = request('zhengji_yuyue','');
      $data['zhuanhua'] = request('zhuanhua','');
      $data['daozhen'] = request('daozhen','');
      $data['zhongzhi_daozhen'] = request('zhongzhi_daozhen','');
      $data['zhengji_daozhen'] = request('zhengji_daozhen','');
      $data['daozhen_zhuanhua'] = request('daozhen_zhuanhua','');
      $data['yuji_shouru'] = request('yuji_shouru','');
      $data['lirun'] = request('lirun',''); 
      
   
      $data['time'] =   date('Y-m-d');
      $data['created_at'] =  date('Y-m-d H:i:s');
      
       $model_amiba = new Amiba();
       $info = $model_amiba->ajaxadd($data);
       return redirect('admin/amiba/index');
   }
   
   
   
   //将bar导出数据
   public  function bar()
   {
        $time =  date('Y-m-d');
        $sql1  = "SELECT guanggao_id,type_id,bar
        FROM i_table
        WHERE
	time = '".$time."'and bar  = 1";
        $bar1= DB::select($sql1);//查询出来每个巴一天支出明细
        $bar1 = json_encode($bar1, true);
        $bar1 = json_decode($bar1, true);
       if(!empty($bar1)){
          foreach($bar1 as $v){
               $v['created_at'] = date('Y-m-d H:i:s');
               $v['time'] = $time;

               $info1 =  DB::table('price_table')->insert([
                   ['bar' => $v['bar'],'guanggao_id' => $v['guanggao_id'],'type_id'=>$v['type_id'],'created_at'=>$v['created_at'],'time'=>$v['time']],
               ]);
            }
       }else{
          
       }
       
       
        $sql2  = "SELECT guanggao_id,type_id,bar
        FROM i_table
        WHERE
	time = '".$time."'and bar  = 2";
        $bar2= DB::select($sql2);//查询出来每个巴一天支出明细
        $bar2 = json_encode($bar2, true);
        $bar2 = json_decode($bar2, true);
         if(!empty($bar2)){
                    foreach($bar2 as $v){
                       $v['created_at'] = date('Y-m-d H:i:s');
                       $v['time'] = $time;

                       $info2 =  DB::table('price_table')->insert([
                           ['bar' => $v['bar'],'guanggao_id' => $v['guanggao_id'],'type_id'=>$v['type_id'],'created_at'=>$v['created_at'],'time'=>$v['time']],
                       ]);
                    }
         }else{
          
        }
       
        
        $sql3  = "SELECT guanggao_id,type_id,bar
        FROM i_table
        WHERE
	time = '".$time."'and bar  = 3";
        
        $bar3= DB::select($sql3);//查询出来每个巴一天支出明细
        $bar3 = json_encode($bar3, true);
        $bar3 = json_decode($bar3, true);
        
         if(!empty($bar3)){
                foreach($bar3 as $v){
                   $v['created_at'] = date('Y-m-d H:i:s');
                   $v['time'] = $time;

                   $info3 =  DB::table('price_table')->insert([
                       ['bar' => $v['bar'],'guanggao_id' => $v['guanggao_id'],'type_id'=>$v['type_id'],'created_at'=>$v['created_at'],'time'=>$v['time']],
                   ]);
                }
        }else{
          
        }
        
             
        $sql4  = "SELECT guanggao_id,type_id,bar
        FROM i_table
        WHERE
	time = '".$time."'and bar  = 4";
        $bar4= DB::select($sql4);//查询出来每个巴一天支出明细
        $bar4= json_encode($bar4, true);
        $bar4 = json_decode($bar4, true);
        
          if(!empty($bar4)){
                    foreach($bar4 as $v){
                       $v['created_at'] = date('Y-m-d H:i:s');
                       $v['time'] = $time;

                       $info4 =  DB::table('price_table')->insert([
                           ['bar' => $v['bar'],'guanggao_id' => $v['guanggao_id'],'type_id'=>$v['type_id'],'created_at'=>$v['created_at'],'time'=>$v['time']],
                       ]);
                    }

          }else{
          
        }
        $sql5  = "SELECT guanggao_id,type_id,bar
        FROM i_table
        WHERE
	time = '".$time."'and bar  = 5";
        $bar5= DB::select($sql5);//查询出来每个巴一天支出明细
        $bar5= json_encode($bar5, true);
        $bar5 = json_decode($bar5, true);
        
        if(!empty($bar5)){
                    foreach($bar5 as $v){
                       $v['created_at'] = date('Y-m-d H:i:s');
                       $v['time'] = $time;

                       $info5 =  DB::table('price_table')->insert([
                           ['bar' => $v['bar'],'guanggao_id' => $v['guanggao_id'],'type_id'=>$v['type_id'],'created_at'=>$v['created_at'],'time'=>$v['time']],
                       ]);
                    }
        } else{
            
        }
         
        $sql6  = "SELECT guanggao_id,type_id,bar
        FROM i_table
        WHERE
	time = '".$time."'and bar  = 6";
        $bar6= DB::select($sql6);//查询出来每个巴一天支出明细
        $bar6= json_encode($bar6, true);
        $bar6 = json_decode($bar6, true);
        
         if(!empty($bar6)){
                    foreach($bar6 as $v){
                       $v['created_at'] = date('Y-m-d H:i:s');
                       $v['time'] = $time;

                       $info6 =  DB::table('price_table')->insert([
                           ['bar' => $v['bar'],'guanggao_id' => $v['guanggao_id'],'type_id'=>$v['type_id'],'created_at'=>$v['created_at'],'time'=>$v['time']],
                       ]);
                    }
         }else{
             
         }
        
        
        
         $sql7  = "SELECT guanggao_id,type_id,bar
        FROM i_table
        WHERE
	time = '".$time."'and bar  = 7";
        $bar7= DB::select($sql7);//查询出来每个巴一天支出明细
        $bar7= json_encode($bar7, true);
        $bar7 = json_decode($bar7, true);
        
       if(!empty($bar7)){
                foreach($bar7 as $v){
                   $v['created_at'] = date('Y-m-d H:i:s');
                   $v['time'] = $time;

                   $info7 =  DB::table('price_table')->insert([
                       ['bar' => $v['bar'],'guanggao_id' => $v['guanggao_id'],'type_id'=>$v['type_id'],'created_at'=>$v['created_at'],'time'=>$v['time']],
                   ]);
                }
       } else {
           
       }
        $sql8  = "SELECT guanggao_id,type_id,bar
        FROM i_table
        WHERE
	time = '".$time."'and bar  = 8";
        $bar8= DB::select($sql8);//查询出来每个巴一天支出明细
        $bar8= json_encode($bar8, true);
        $bar8 = json_decode($bar8, true);
         if(!empty($bar8)){
                    foreach($bar8 as $v){
                       $v['created_at'] = date('Y-m-d H:i:s');
                       $v['time'] = $time;

                       $info8 =  DB::table('price_table')->insert([
                           ['bar' => $v['bar'],'guanggao_id' => $v['guanggao_id'],'type_id'=>$v['type_id'],'created_at'=>$v['created_at'],'time'=>$v['time']],
                       ]);
                    }
         } else {
             
         }
       
        
        $sql9  = "SELECT guanggao_id,type_id,bar
        FROM i_table
        WHERE
	time = '".$time."'and bar  = 9";
        $bar9= DB::select($sql9);//查询出来每个巴一天支出明细
        $bar9= json_encode($bar9, true);
        $bar9 = json_decode($bar9, true);
        
        if(!empty($bar9)){
                foreach($bar9 as $v){
                   $v['created_at'] = date('Y-m-d H:i:s');
                   $v['time'] = $time;

                   $info9 =  DB::table('price_table')->insert([
                       ['bar' => $v['bar'],'guanggao_id' => $v['guanggao_id'],'type_id'=>$v['type_id'],'created_at'=>$v['created_at'],'time'=>$v['time']],
                   ]);
                }
        } else {
            
        }
      
        
        
        $sql  = "SELECT bar,SUM(lirun) as lirun,SUM(daozhen) as daozhen, SUM(yuji_shouru) as yuji_shouru
        FROM i_table
        WHERE
	time = '".$time."'  GROUP BY bar";
       
        $list = DB::select($sql);//查询出来每个巴一天收入明细 
        $list = json_encode($list, true);
        $list = json_decode($list, true);
        
        foreach($list as $v){
  
           $v['created_at'] = date('Y-m-d H:i:s');
           $v['time'] = $time;
           
           $info =  DB::table('count_amb')->insert([
               ['bar' => $v['bar'],'daozhen' => $v['daozhen'],'lirun'=>$v['lirun'],'yuji_shouru'=>$v['yuji_shouru'],'created_at'=>$v['created_at'],'time'=>$v['time']],
           ]);
        }
        
       if($info){
           Flash::success(trans('alerts.amiba_count.created_success'));
            return redirect('admin/amiba/count');
            return true;
       }else{
            Flash::error(trans('alerts.amiba_count.created_error'));
            return redirect('admin/amiba/count');
            return false;
       }
     
   }
   
  
   
   public  function  project_status()
   {
        $time =  date('Y-m-d');
        
        
        
        $time =  date('Y-m-d');
        $sql1  = "SELECT guanggao_id,type_id,status
        FROM i_table
        WHERE
	time = '".$time."'and status  = 1";
        $bar1= DB::select($sql1);//查询出来每个巴一天支出明细
        $bar1 = json_encode($bar1, true);
        $bar1 = json_decode($bar1, true);
        
       if(!empty($bar1)){
          foreach($bar1 as $v){
               $v['created_at'] = date('Y-m-d H:i:s');
               $v['time'] = $time;

               $info1 =  DB::table('price_table')->insert([
                   ['status' => $v['status'],'guanggao_id' => $v['guanggao_id'],'type_id'=>$v['type_id'],'created_at'=>$v['created_at'],'time'=>$v['time']],
               ]);
            }
       }else{
          
       }
       
       
        $sql2  = "SELECT guanggao_id,type_id,status
        FROM i_table
        WHERE
	time = '".$time."'and status  = 2";
        $bar2= DB::select($sql2);//查询出来每个巴一天支出明细
        $bar2 = json_encode($bar2, true);
        $bar2 = json_decode($bar2, true);
         if(!empty($bar2)){
                    foreach($bar2 as $v){
                       $v['created_at'] = date('Y-m-d H:i:s');
                       $v['time'] = $time;

                       $info2 =  DB::table('price_table')->insert([
                           ['status' => $v['status'],'guanggao_id' => $v['guanggao_id'],'type_id'=>$v['type_id'],'created_at'=>$v['created_at'],'time'=>$v['time']],
                       ]);
                    }
         }else{
          
        }
       
        
        $sql3  = "SELECT guanggao_id,type_id,status
        FROM i_table
        WHERE
	time = '".$time."'and status  = 3";
        
        $bar3= DB::select($sql3);//查询出来每个巴一天支出明细
        $bar3 = json_encode($bar3, true);
        $bar3 = json_decode($bar3, true);
        
         if(!empty($bar3)){
                foreach($bar3 as $v){
                   $v['created_at'] = date('Y-m-d H:i:s');
                   $v['time'] = $time;

                   $info3 =  DB::table('price_table')->insert([
                       ['status' => $v['status'],'guanggao_id' => $v['guanggao_id'],'type_id'=>$v['type_id'],'created_at'=>$v['created_at'],'time'=>$v['time']],
                   ]);
                }
        }else{
          
        }
        
             
        $sql4  = "SELECT guanggao_id,type_id,status
        FROM i_table
        WHERE
	time = '".$time."'and status  = 4";
        $bar4= DB::select($sql4);//查询出来每个巴一天支出明细
        $bar4= json_encode($bar4, true);
        $bar4 = json_decode($bar4, true);
        
          if(!empty($bar4)){
                    foreach($bar4 as $v){
                       $v['created_at'] = date('Y-m-d H:i:s');
                       $v['time'] = $time;

                       $info4 =  DB::table('price_table')->insert([
                           ['status' => $v['status'],'guanggao_id' => $v['guanggao_id'],'type_id'=>$v['type_id'],'created_at'=>$v['created_at'],'time'=>$v['time']],
                       ]);
                    }

          }else{
          
        }
 
        
        $sql  = "SELECT status,SUM(lirun) as lirun,SUM(daozhen) as daozhen, SUM(yuji_shouru) as yuji_shouru
        FROM i_table
        WHERE
	time = '".$time."'  GROUP BY status";
       
        $list = DB::select($sql);//查询出来每个巴一天收入明细 
        $list = json_encode($list, true);
        $list = json_decode($list, true);
        
        foreach($list as $v){
  
           $v['created_at'] = date('Y-m-d H:i:s');
           $v['time'] = $time;
           
           $info =  DB::table('count_project')->insert([
               ['status' => $v['status'],'daozhen' => $v['daozhen'],'lirun'=>$v['lirun'],'yuji_shouru'=>$v['yuji_shouru'],'created_at'=>$v['created_at'],'time'=>$v['time']],
           ]);
        }
        
       if($info){
           Flash::success(trans('alerts.amiba_count.created_success'));
            return redirect('admin/amiba/project');
            return true;
       }else{
            Flash::error(trans('alerts.amiba_count.created_error'));
            return redirect('admin/amiba/project');
            return false;
       }
   }
   
   //导出数据
   public function excel()
   {
        ini_set('memory_limit','500M');
        set_time_limit(0);//设置超时限制为0分钟
        
        
    $cellData = DB::table('i_table')->select('bar','yuji_shouru','lirun','time')->limit(5)->get();
    $cellData = json_encode($cellData, true);
    $cellData = json_decode($cellData, true);
        
    $cellData[0] = array('阿米巴','预计收入','利润','时间');  
       for($i=0;$i<count($cellData);$i++){  
           $cellData[$i] = array_values($cellData[$i]);  
           $cellData[$i][0] = str_replace('=',' '.'=',$cellData[$i][0]);  
       }  
        //dd($cellData);
        Excel::create('阿米巴每日报表',function($excel) use ($cellData){
            $excel->sheet('score', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        
       Flash::success(trans('alerts.excel.created_success'));
       return redirect('admin/amiba/all');

   }
   
  
}
